<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\agency\controller;


use app\agency\service\NodeService;
use app\common\facade\Arr;
use app\common\services\UserService;
use app\common\services\wechattemplate\UserTemplateService;
use library\Controller;
use library\tools\Data;
use think\Console;
use think\Db;
use think\exception\HttpResponseException;
use app\common\facade\TransItem;
use app\common\services\JobService;
use app\common\services\PublishJobService;
use app\common\services\JobOptionService;

/**
 * 会员管理
 * Class Index
 * @package app\agency\controller
 */
class Member extends AgentBase
{
    public $table = 'User';

    /**
     * 会员概况
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        //获取当前代理商所在城市
        $user=$this->agentUser;
        $data['provinceid']=$user['provinces_id'];
        $data['cityid']=$user['city_id'];

        $result['total']=DB::name($this->table)->where($data)->count();//全部会员
        $result['total_no']=DB::name($this->table)->where($data)->where('type=0')->count();//未提交认证

        $result['total_user']=DB::name($this->table)->where($data)->where('type=1 and status=2')->count();//个人认证成功
        $result['total_business']=DB::name($this->table)->where($data)->where('type=2 and status=2 ')->count();//商户认证成功
        $result['pre_user']=DB::name($this->table)->where($data)->where('status=1   and type=1')->count();
        $result['pre_business']=DB::name($this->table)->where($data)->where('status=1   and type=2')->count();
        $result['deny_user']=DB::name($this->table)->where($data)->where('status=3   and type=1')->count();
        $result['deny_business']=DB::name($this->table)->where($data)->where('status=3   and type=2')->count();
        $this->assign('result',$result);

        $this->fetch();

    }

     /**
     * 会员列表
     * @auth true
     * @menu true
     * @throws \ReflectionException
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function memberList(){

        $this->title = '会员列表';
        $status = $this->request->param('status', 0);
        if($status!=0){
            $data[]=['status','EQ',$status];
            if($status==1){
                $this->title = '待认证会员';
            }elseif ($status==2) {
                $this->title = '审核通过会员列表';
            }else {
                $this->title = '审核拒绝会员列表';
            }
        }else{
            $data[]=['status','IN',[1,2,3,0]];
        }

        $result=$this->agentUser;
        $data[]=['provinceid','EQ',$result['provinces_id']];
        $data[]=['cityid','EQ',$result['city_id']];
        $data[]=['type','IN',[1,0]]; 

        $query = $this->_query($this->table)
            ->where($data)
            ->like('title')
            ->equal('id,stauts,name,is_subscribe');
        $query->order('id desc')->page();



    }
    /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _memberList_page_filter(&$data)
    {

        $this->clist = Db::name('UserExtend')->field("user_id,true_name")->select();
        foreach ($data as &$vo) {
            
            foreach ($this->clist as $cate) if ($cate['user_id'] === $vo['id']) $vo['cate'] = $cate;

        }

    }



    /**
     * 会员认证审核
     * @auth true
     */
    public function auditApply(){

        $id = $this->request->param('id', 0);
        $status = $this->request->param('status', 0);
        if($status==2){
            //认证通过
            $this->_save($this->table, ['status' => '2']);
        }else{
            $message=$this->request->param('content');
            if(empty($message)){
                $this->error('请输入拒绝理由');
            }else{
                //认证通过
                $this->_save($this->table, ['status' => '3',"replay"=>$message]);
            }
        }
    }


    /**
     * 审核用户认证发送模版消息
     * @param $result
     *
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _auditApply_save_result($result)
    {
        if ($result) {
            $id = $this->request->param('id', 0);
            $userInfo = (new UserService())->userInfo($id, ['extend']);
            $status = $this->request->param('status', 0);
            if($status==2) {
                if($userInfo['is_send']==0){
                    //3： 送发布豆
                    $user_wallet=DB::name('user_wallet')->where("user_id",$userInfo['id'])->find();

                    Data::save(
                        'user_wallet',
                        [
                            'id'                =>$user_wallet['id'],
                            'user_id'           => $userInfo['id'],
                            'publish_bean'      => sysconf('approve_personal_bean')+$user_wallet['publish_bean'],
                        ]
                    );

                    //发布豆流水记录
                    if ((sysconf('approve_personal_bean') ?: 0)) {
                        Data::save(
                            'user_recharge_log',
                            [
                                'user_id'           => $userInfo['id'],
                                'recharge_money'    => 0,
                                'publish_bean'      => sysconf('approve_personal_bean') ?: 0,
                                'recharge_rate'     => 0,
                                'give_publish_bean' => 0,
                                'give_rate'         => 0,
                                'type'              => 1,
                                'kind'              => 7
                            ]
                        );
                    }
                    //更新user表
                    Data::save(
                        'user',
                        [
                            'id'           => $userInfo['id'],
                            'is_send'      => 1,
                        ]
                    );
                }
                (new UserTemplateService())->approveSuccess($userInfo);
            } else {
                $message=$this->request->param('content');
                (new UserTemplateService())->approveFalse($userInfo, $message);
            }

        }
        return $result;
    }




    public function memberDeny(){
        $this->title = '会员认证拒绝';
        $result=$this->request->param();
        $this->assign('result',$result);
        $this->fetch('user_deny');
    }

    /**
     * 会员资料查看
     *@auth true
     */
    public function memberInfo(){
       

        $id=$this->request->param('id');
       /* $user=DB::name("UserExtend")
            ->alias('ue')
            ->where('ue.user_id',$id)
            ->join('User u', 'u.id = ue.user_id')
            ->join('School s', 's.id = ue.education_school')
            ->join('SchoolMajor sm', 'sm.id = ue.education_major')
            ->field('ue.* ,u.email,s.name as school,sm.name as schoolmajor')
            ->find();*/
        $user=DB::name("UserExtend")
            ->alias('ue')
            ->where('ue.user_id',$id)
            ->join('User u', 'u.id = ue.user_id')
            ->field('ue.* ,u.email')
            ->find();
        if($user['education_school']!=0){
            $user['school']=DB::name('School')->where("id",$user['education_school'])->value('name');
        }else{
            $user['school']="其他";
        }
        if($user['education_major']!=0){
            $user['schoolmajor']=DB::name('SchoolMajor')->where("id",$user['education_major'])->value('name');
        }else{
            $user['schoolmajor']="其他";
        }
        if(!empty($user)){
            $user['sex'] = TransItem::transGender(Arr::get($user, 'true_sex'));
            /*$user['transProfession'] = TransItem::transProfession(Arr::get($user, 'profession'));
            $user['transEducation'] = TransItem::transEducation(Arr::get($user, 'education_class'));*/
        }
        if($user['profession']!=0){
            $user['transProfession'] = TransItem::transProfession(Arr::get($user, 'profession'));
        }else{
            
            $user['transProfession']="其他";
        }
        if($user['education_class']!=0){
            $user['transEducation'] = TransItem::transEducation(Arr::get($user, 'education_class'));
        }else{
            $user['transEducation']="其他";
        }
        $this->assign('vo',$user);

        $this->fetch('member_info');
    }






}
